from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.conf import settings
from PeopleInfo.models import *
from Dashboard.models import DailySubmitInfo
from Dashboard.tasks import *

import requests
# Create your views here.


def index(request):
    for human in Gxjg0101.objects.filter(zj__isnull=False):
        url = human.zp
        if not url:
            continue
        save_path = settings.STATIC_ROOT + '{0}.jpg'.format(human.xgh)  # 定义一个存储地址在d盘，爬取下来的图片命名为abc
        r = requests.get(url)
        r.status_code  # 返回200，说明正常
        # 图片是一个二进制格式
        with open(save_path, 'wb') as f:  # 打开存储的文件，并将它定义为文件标识符f
            f.write(r.content)  # 将返回的内容写到这个文件中，r.content表示响应内容的二进制形式
    return HttpResponse('success')


def log_record(request, token):
    import json
    if token == settings.LOG_RECORD_TOKEN:
        data = request.GET
        LogRecord.objects.create(text=json.dumps(data))
    return HttpResponse('success')


def test(request):
    return HttpResponse("this is master server")
